
#include "stdafx.h"
#include "wf_getpoint.h"

#include "pf_snapper.h"



WF_GetPoint::WF_GetPoint(PF_Container& container,
        PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Get Point",
                           container, graphicView) {
    completed = false;
    mesage = tr("Specify a point");

}


void WF_GetPoint::init(int status) {
    PF_ActionInterface::init(status);
}


void WF_GetPoint::trigger() {

    PF_DEBUG->print("QC_ActionGetPoint::trigger()");
    completed = true;
}


void WF_GetPoint::mouseMoveEvent(PF_MouseEvent* e) {
    PF_DEBUG->print("QC_ActionGetPoint::mouseMoveEvent begin");
    if (getStatus()==SetReferencePoint ||
            getStatus()==SetTargetPoint) {

        PF_Vector mouse = snapPoint(e);
        switch (getStatus()) {
        case SetReferencePoint:
            targetPoint = mouse;
            break;

        case SetTargetPoint:
            if (referencePoint.valid) {
                targetPoint = mouse;
                deletePreview();
                PF_Line *line =new PF_Line(preview,
                                       PF_LineAttribute(referencePoint, mouse));
                line->setPen(PF_Pen(PF_Color(0,0,0), LX_Define::Width00, LX_Define::DotLine ));
                preview->addEntity(line);
                PF_DEBUG->print("QC_ActionGetPoint::mouseMoveEvent: draw preview");
                drawPreview();
                preview->addSelectionFrom(*container);
            }
            break;

        default:
            break;
        }
    }

    PF_DEBUG->print("QC_ActionGetPoint::mouseMoveEvent end");
}



void WF_GetPoint::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        PF_CoordinateEvent ce(snapPoint(e));
        coordinateEvent(&ce);
    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        init(getStatus()-1);
    }
}

void WF_GetPoint::coordinateEvent(PF_CoordinateEvent* e) {

    if (e==NULL) {
        return;
    }

    PF_Vector pos = e->getCoordinate();

    switch (getStatus()) {
    case SetReferencePoint:
    case SetTargetPoint:
        targetPoint = pos;
        graphicView->moveRelativeZero(targetPoint);
        trigger();
        finish();
        break;

    default:
        break;
    }
}


void WF_GetPoint::updateMouseButtonHints() {
    switch (getStatus()) {
    case SetReferencePoint:
    case SetTargetPoint:
        PF_DIALOGFACTORY->updateMouseWidget(mesage, tr("Cancel"));
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}


void WF_GetPoint::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}


void WF_GetPoint::updateToolBar() {
    switch (getStatus()) {
    case SetReferencePoint:
    case SetTargetPoint:
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
        break;
    default:
        PF_DIALOGFACTORY->requestPreviousMenu();
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}


void WF_GetPoint::setBasepoint(QPointF* basepoint){
    referencePoint.x = basepoint->x();
    referencePoint.y = basepoint->y();
    setStatus(SetTargetPoint);
}


void WF_GetPoint::setMesage(QString msg){
    mesage = msg;
}


void WF_GetPoint::getPoint(QPointF *point) {
    if (completed) {
        point->setX(targetPoint.x);
        point->setY(targetPoint.y);
    } else {
        point->setX(0.0);
        point->setY(0.0);
    }
}

// EOF
